﻿using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component; 

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.Shop
{
    /// <summary>
    /// frmShop.xaml 的交互逻辑
    /// </summary>
    public partial class frmShop : BasePage
    {
        public frmShop()
        {
            InitializeComponent();
        }


        public override void CallBack(object model)
        {
            base.CallBack(model);

            LoadShop();
        }

        private void btnSearchCompany_Click(object sender, RoutedEventArgs e)
        {
            this.LoadCompany();
        }

        //private void dgvCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (this.dgvCompany.SelectedValue != null)
        //    { 
        //        this.LoadShop();
        //    }
        //}

        private void btnShopAdd_Click(object sender, RoutedEventArgs e)
        {
            ShopEntity shopEntity = new ShopEntity();
            shopEntity.CompanySN = CompanySN;
            base.Navigate("/View/CenterInfo/Shop/frmShopEdit", shopEntity);
        }

        private void btnShopEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvShop.SelectedItems.Count > 0)
            {
                ShopEntity shop = (ShopEntity)this.dgvShop.SelectedValue;
                base.Contain.Navigate("/View/CenterInfo/Shop/frmShopEdit", shop.Clone());
            }
        }

        private async void btnShopDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("请确认是否要删除", "删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                ShopEntity company = (ShopEntity)this.dgvShop.SelectedValue;
                DataDto dto = await GL.HttpClientManage.ShopDelete(company.SN);
                if (dto.error == null)
                {
                    this.LoadShop();
                }
            }
        }

        private void btnShopFind_Click(object sender, RoutedEventArgs e)
        {
            LoadShop();
        }

        private async void LoadCompany()
        {
            string CompanyName = this.txtCompany.Text;
            CompanyListDto dto = await GL.HttpClientManage.QueryCompanyList(CompanyName);
            TreeViewItem tviRoot = (TreeViewItem)this.tvCompany.Items[0];
            tviRoot.ItemsSource = null;
            if (dto.error == null)
            {
                tviRoot.ItemsSource = dto.data;
                tviRoot.IsExpanded = true;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }

        }


        private async void LoadShop()
        {
            int companySN = (int)this.CompanySN;
            if (companySN > 0)
            {
                ShopListDto dto = await GL.HttpClientManage.ShopQuery(companySN, txtShop.Text);
                if (dto.error == null)
                {
                    this.dgvShop.ItemsSource = dto.data;
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
        }

        private int CompanySN
        {
            get
            {
                int companySN = 0; 
                if (this.tvCompany.SelectedValue is CompanyEntity)
                {
                    companySN = (int)((CompanyEntity)this.tvCompany.SelectedValue).SN;
                }
                return companySN;
            }
        }

        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadCompany();
        }
    }
}
